<?php

namespace App\Http\Resources;

use App\Repositories\SuggestRepo;
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;

class SuggestResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $status = $this->status;
        $allStatus = SuggestRepo::status();
        $statusText = $allStatus[$status] ?? '';
        return [
            'id' => $this->id,
            'title' => $this->title,
            'level' => $this->level,
            'project_id' => $this->project_id,
            'project_name' => $this->project->name ?? '',
            'task_id' => $this->task_id,
            'task_name' => $this->task->name ?? '',
            'create_user_id' => $this->create_user_id,
            'create_user_name' => $this->create_user->realname ?? '',
            'charge_user_id' => $this->charge_user_id,
            'charge_user_name' => $this->charge_user->realname ?? '',
            'description' => $this->description,
            'source' => $this->source,
            'status' => $status,
            'status_text' => $statusText,
            'button_show' => $this->buttonShow(),
            'button_modify' => $this->buttonModify(),
            'button_done' => $this->buttonDone(),
            'button_close' => $this->buttonClose(),
            'button_delete' => $this->buttonDelete(),
            'resolve' => $this->getResolve(),
            'resolve_user_names' => $this->getResolveUserNames(),
            'resolve_time' => $this->getResolveDate(),
            'create_time' => Carbon::parse($this->create_time)->toDateTimeString(),
            'update_time' => Carbon::parse($this->update_time)->toDateTimeString(),
        ];
    }

    /**
     * 问题解决时间
     */
    private function getResolveDate()
    {
        $resolve = $this->resolve;
        if (empty($resolve)) {
            return '';
        }
        try {
            return Carbon::parse($resolve->resolve_date)->toDateTimeString();
        } catch (\Exception $e) {
            return '';
        }
    }

    /**
     * 问题解决人名字
     */
    private function getResolveUserNames()
    {
        $users = $this->getResolveUsers();
        if (empty($users)) {
            return [];
        }
        return collect($users)->pluck('name')->toArray();
    }

    /**
     * 问题解决数据
     */
    private function getResolve()
    {
        $resolve = $this->resolve;
        if (empty($resolve)) {
            return null;
        }
        try {
            $resolveDate = Carbon::parse($resolve->resolve_date)->toDateTimeString();
        } catch (\Exception $e) {
            $resolveDate = '';
        }
        return [
            'category' => $resolve->category,
            'resolve_date' => $resolveDate,
            'reason' => $resolve->reason,
            'scheme' => $resolve->scheme,
            'description' => $resolve->description,
            'users' => $this->getResolveUsers()
        ];
    }

    /**
     * 问题解决人
     */
    private function getResolveUsers()
    {
        $resolveUsers = $this->resolve->users ?? null;
        if (empty($resolveUsers)) {
            return [];
        }
        $users = [];
        foreach ($resolveUsers as $resolveUser) {
            $user = $resolveUser->user;
            if (empty($user)) {
                continue;
            }
            $users[] = [
                'id' => $user->id,
                'name' => $user->realname
            ];
        }
        return $users;
    }

    /**
     * 删除按钮是否显示
     * true: 显示， false: 不显示
     */
    private function buttonDelete()
    {
        $createUserId = $this->create_user_id;
        if ($createUserId != registry('user_id')) {
            return false;
        }
        return true;
    }

    /**
     * 关闭按钮是否显示
     * true: 显示， false: 不显示
     */
    private function buttonClose()
    {
        $createUserId = $this->create_user_id;
        if ($createUserId != registry('user_id')) {
            return false;
        }
        $status = $this->status;
        if ($status == SuggestRepo::STATUS_CLOSED) {
            return false;
        }
        return true;
    }

    /**
     * 完成按钮是否显示
     * true: 显示， false: 不显示
     */
    private function buttonDone()
    {
        $createUserId = $this->create_user_id;
        if ($createUserId != registry('user_id')) {
            return false;
        }
        $status = $this->status;
        if ($status != SuggestRepo::STATUS_PENDING) {
            return false;
        }
        return true;
    }

    /**
     * 编辑按钮是否显示
     * true: 显示， false: 不显示
     */
    private function buttonModify()
    {
        $createUserId = $this->create_user_id;
        if ($createUserId != registry('user_id')) {
            return false;
        }
        $status = $this->status;
        if ($status != SuggestRepo::STATUS_PENDING) {
            return false;
        }
        return true;
    }

    /**
     * 查看按钮是否显示
     * true: 显示， false: 不显示
     */
    private function buttonShow()
    {
        $status = $this->status;
        return true;
    }
}
